package cc.linker.steplink.crm.commons.oss;


import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * 日期工具
 * @since

 */
public class DateUtils {
	private static Log log = LogFactory.getLog(DateUtils.class);

	
	/**
	 * 获取当前Date
	 * @return date
	 */
	public static Date getCurrentDate() {
		Date today = new Date();
		return today;
	}	

	
	/**
	 * 获取当前Calendar
	 * @return 当前Calendar
	 */
	public static Calendar getCurrentCalendar() {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date());
		return calendar;
	}

	/**
	 * 
	 * @param date
	 * @return Calendar
	 */
	public static Calendar getCalendar(Date date) {
		Calendar cal = new GregorianCalendar();
		cal.setTime(date);
		return cal;
	}

	/**
	 * 
	 * <p>
	 * @param formatString
	 * @param strDate
	 */
	public static Date convertStringToDate(String formatString, String strDate) {
		SimpleDateFormat df;
		Date date = null;
		df = new SimpleDateFormat(formatString);
		
		try {
			date = df.parse(strDate);
		} catch (ParseException pe) {
			if (log.isErrorEnabled()) {
				log.error("ParseException: " + pe);
			}
			// throw new ParseException(pe.getMessage(), pe.getErrorOffset());
		}

		return (date);
	}

	/**
	 * 
	 * <p>
	 * @param formatString
	 * @param date
	 */
	public static String formatDate(String formatString, Date date) {
		SimpleDateFormat df = null;
		String returnValue = "";

		if (date == null) {
			if (log.isErrorEnabled()) {
				log.error("aDate is null!");
			}
		} else {
			df = new SimpleDateFormat(formatString);
			returnValue = df.format(date);
		}

		return returnValue;
	}
}
